#include<stdio.h>
#include<stdlib.h>
struct node
{
int val;
struct node *next;
};

struct node *insert1(struct node *list,int n)
{
struct node *temp;
temp=malloc(sizeof(struct node));
temp->val=n;
temp->next = list;
return temp;
}

void print1(struct node *list)
{
while (list!=NULL)
{
printf("values are:%d \n",list->val);
list=list->next;
}
}
//1.reverse a linked list
struct node *reverse1(struct node *list)
{
	struct node *temp, *t,*t2;
	t=malloc(sizeof(struct node));
	t->val=list->val;
	t->next=NULL;
	list=list->next;
	temp=malloc(sizeof(struct node));
	temp->val=list->val;
	temp->next=t;
	while((list->next)!=NULL)
	{
		t2=malloc(sizeof(struct node));
		list=list->next;
		t2->val=list->val;
		t2->next=temp;
		temp=t2;
	}
	return temp;
}
//2.to append an element
struct node *append1(struct node *list,int n)
{
struct node *a;

while(a->next!=NULL)
{
list=list->next;
if(list->next==NULL)
	{
	a=malloc(sizeof(struct node));
	a->val=n;
	list->next=a;
	a->next=NULL;	
	}
}
return a;
}

//3.to return the index of the requisite value.
int index_val1(struct node *list,int x)
{
int c=0;
while(list->next!=NULL)
{
if(list->val==x)
	break;
c++;
list=list->next;
}
return c;
}

//4.count the occurances of an element in the linked list
int count1(struct node *list,int x)
{
int c=0;
while(list->next!=NULL)
{
if(list->val==x)
	c++;
list=list->next;
};
return c;
}

//1. to find max.
int max_array(int a[],int n)
{
int i,max;
max=a[0];
for(i=0;i<n;i++)
{
	if(a[i]>max)
		max=a[i];
}
return max;
}

// 2. to find min
int min_array(int a[],int n)
{
int i,min;
min=a[0];
for(i=0;i<n;i++)
{
	if(a[i]<min)
		min=a[i];
}
return min;
}

//3. reversing an array
void reverse_array(int a[],int n)
{
int i,temp;
for(i=0;i<(n/2);i++)
{
temp=a[i];
a[i]=a[n-1-i];
a[n-1-i]=temp;
}
}
//4. to return the index of requisite element
int index_val(int a[], int n , int val)
{
int i,j,flag=0;
for(i=0;i<n;i++)
{
if(a[i]==val)
	{
	return i;
	flag=1;
	break;
	}
else
	continue;
}
if(flag==0)
	return -1;
}
//5.count the occurrence of a number in an array
int count_val(int a[],int n, int val)
{
int i,t=0;
for(i=0;i<n;i++)
{
	if(a[i]==val)
		t++;
	
}
return t;
}
//printing an array
void print_array(int a[],int n)
{
int i;
printf("[");
for(i=0;i<n;i++)
	printf("%d,",a[i]);
printf("]");
} 
//accepting values in a array;
void initialise(int a[],int n)
{
int i;
for(i=0;i<n;i++)
{
printf("enter value of element %d:", i);
scanf("%d",&a[i]);
}
}
//7.sort the arrays
void sort_array(int a[],int n)
{
int i,j,temp;
for(i=0;i<n;i++)
	for(j=i;j<n;j++)
	{
	if(a[j]<a[i])
		{
		temp=a[i];
		a[i]=a[j];
		a[j]=temp;
		}
}}
//8.pop function (i=index)
void delete_index(int a[], int n, int i)
{
int j;
for(j=i;j<n;j++)
{
a[j]=a[j+1];
}
}

//6.deletion of an element from a list of array
int del_array1(int a[],int n,int x)
{
int j;
int flag;
for(j=0;j<n;j++)
{
if(a[j]==x)
	{
	delete_index(a,n,j);
	flag=1;
	break;
	}
else
	flag=0;
}
return flag;
}
//9.append elemnt at end.
void array_append(int a[],int n, int x)
{
int i;
*(a+n)=x;
}
//10.insert element at a specific position in an array
void insert(int a[],int n,int i,int x)
{
int j;
for(j=n;j>i;j--)	
	*(a+j)=*(a+j-1);
*(a+i)=x;
}
//11.extending the array by appending another array to the present array
void extend(int a[],int n,int b[],int m)
{
int i,j;
for(i=n,j=0;i<(n+m),j<m;i++,j++)
{
*(a+i)=b[j];
}
}
//main function for array...
void array()
{
int p=0;
int n,i, choice,x,y;
printf("enter the size of array:");
scanf("%d",&n);
int a[n];
initialise(a,n);
while(p==0)
{
printf("\n ENTER THE FUNCTION U WANT TO PERFORM:\n1.min of array\n2.max of array\n3.reverse\n4.index\n5.count\n6.deletion\n7.sorting\n8.pop\n9.append\n10.insert at index\n11.extend\n");
scanf("%d",&choice);
switch(choice)
{
case 1: x=max_array(a,n);
	printf("max number is:%d", x);
	break;
case 2: x=min_array(a,n);
	printf("min number is:%d",x);
	break;	
case 3: reverse_array(a,n);
	break;
case 4: printf("enter value to be searched for:");
	scanf("%d",&x);
	y=index_val(a,n,x);
	printf("index:%d",y);
	break;
case 5: printf("enter the value whose occurance is to be counted:");
	scanf("%d",&x);
	y=count_val(a,n,x);
	printf("occurance:%d",y);
	break;
case 6: printf("enter the element that u wanna delete from the array:");
	scanf("%d",&x);
	y=del_array1(a,n,x);
	if(y==1)
		n=n-1;
	else
		printf("the element that you asked for is not in the array");
	break;
case 7: sort_array(a,n);
	break;
case 8: printf("enter the element number that you want to pop:");
	scanf("%d",&x);
	if(x<n)
	{
	delete_index(a,n,x);
	n=n-1;
	}
	else
	printf("the index provided is more than the number of elements in the array:");
	break;
case 9: printf("enter the elemnt u want to append:");
	scanf("%d",&x); 
	array_append(a,n,x);
	n=n+1;
	break;
case 10:printf("enter the index,element that u want to insert<index,element>:");
	scanf("%d,%d",&x,&y);
	if(x<n)
		{insert(a,n,x,y);n=n+1;}
	else
		printf("the index provided is greater than the max index of the array:");
	break;
case 11: printf("enter the size of array that u will be extending to the given array:");
	scanf("%d",&x);
	int b[x];
	initialise(b,x);
	extend(a,n,b,x);
	n=n+x;
	break;
}
printf("\nthe array is:");
print_array(a,n);
printf("\n");
printf("do u want to continue performing the operation on your array?(y=0/n=1)");
scanf("%d",&p);
};
}
//main function for linked list
void linked_list()
{
int x,y,i,tnum,z;
struct node *a=NULL;
printf("lenght of link list:");
scanf("%d",&x);
for (i=0;i<x;i++)
{
printf("enter the value:");
scanf("%d",&tnum);
a=insert1(a,tnum);
} 

printf("\nMenu\n1.reverse\n2.append\n3.find index\n4.count\nselect an operation that u would like to perform on the given link list created:");
scanf("%d",&z);
switch(z)
{
case 1: reverse1(a);
	break;
case 2: printf("enter the value that you want to append:");
	scanf("%d",&x);
	append1(a,x);
	break;
case 3: printf("enter the value whose index you want to find out:");
	scanf("%d",&y);
	z=index_val1(a,y);
	printf("the index position of the value u asked for is:%d\n",z);
	break;
case 4:printf("enter the value whose occurance you want to calculate:");
	scanf("%d",&y);
	z=count1(a,y);
	printf("the number of times %d comes in the linked list comes is:%d\n",y,z);
	break;
default:printf("you entered a wrong option\n");
	break;
}
printf("the values currently stored in the linked list are(in right order are):\n");
print1 (a);
}

void main()
{
int x;
printf("1.ARRAYS\n2.LINKED LIST\nchoice:");
scanf("%d",&x);
if(x==1)
	array();
else if(x==2)
	linked_list();
else
	printf("--wrong option--");

}

